﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NHibData.Attributes;
using PagedList;
using StudyEnglish.Application.Filter;
using StudyEnglish.Model;
using StudyEnglish.Models;
using StudyEnglish.Service;

namespace StudyEnglish.Controllers
{
    public class LibraryController : BaseController
    {
        //
        // GET: /Library/

        private static SmartFilter<File> CreateFilter()
        {
            var allItems = DataService.Dictionary.FindAllItems().OrderBy(x=>x.Name);
            var allLevels = (from x in allItems where x.DictType == EDictType.Level select x).ToList();
            var allCatgories = (from x in allItems where x.DictType == EDictType.Category select x).ToList();
            var allSubCategories = (from x in allItems where x.DictType == EDictType.SubCategory select x).ToList();
            var allTopics = (from x in allItems where x.DictType == EDictType.Topic select x).ToList();
            var allTypes = (from x in allItems where x.DictType == EDictType.Type select x).ToList();

            var filter = new SmartFilter<File> { InitialUrl = "/Library/Index/?" };

            var cat1 = new FilterCategory<File>("Levels");
            foreach (var item in allLevels)
            {
                var currentItem = item;
                cat1.AddItem(new FilterItem<File>(item.Name, x => x.RelatedDictionaryItems.Contains(currentItem)));
            }

            var cat2 = new FilterCategory<File>("Categories");
            foreach (var item in allCatgories)
            {
                var currentItem = item;
                cat2.AddItem(new FilterItem<File>(item.Name, x => x.RelatedDictionaryItems.Contains(currentItem)));
            }

            var cat3 = new FilterCategory<File>("Subcategories");
            foreach (var item in allSubCategories)
            {
                var currentItem = item;
                cat3.AddItem(new FilterItem<File>(item.Name, x => x.RelatedDictionaryItems.Contains(currentItem)));
            }

            var cat4 = new FilterCategory<File>("Topics");
            foreach (var item in allTopics)
            {
                var currentItem = item;
                cat4.AddItem(new FilterItem<File>(item.Name, x => x.RelatedDictionaryItems.Contains(currentItem)));
            }

            var cat5 = new FilterCategory<File>("Types");
            foreach (var item in allTypes)
            {
                var currentItem = item;
                cat5.AddItem(new FilterItem<File>(item.Name, x => x.RelatedDictionaryItems.Contains(currentItem)));
            }
            
            filter.AddCategory(cat1);
            filter.AddCategory(cat2);
            filter.AddCategory(cat3);
            filter.AddCategory(cat4);
            filter.AddCategory(cat5);

            return filter;
        }

        public ActionResult Index(string filter, int? page)
        {
            var smartFilter = CreateFilter();
            ViewBag.Filter = smartFilter;
            var model = new LibraryEditorModel();
            var files = DataService.FileService.FilesLinq;

            


            if (string.IsNullOrEmpty(filter)==false)
            {
                smartFilter.ApplyRequestFilter(filter);
                var condition = smartFilter.GetFilterWhereCondition();
                files = files.Where(condition);
            }

            var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
            var onePageOfFiles = files.ToPagedList(pageNumber, 25); // will only contain 25 products max because of the pageSize

            return View(onePageOfFiles);
        }

        [Transaction]
        public string UpdateFile(string value, int? row_id, int? column)
        {
            if (row_id == null || column == null)
                throw new ArgumentNullException("row_id or column can't be null");


            File file = DataService.FileService.GetFileById(row_id.Value);
            switch (column)
            {
                case 1:
                    file.InitialFileName = value;
                    break;
                case 2:
                    file.Comments = value;
                    break;
                case 3:
                    file.Tags = value;
                    break;
            }
            DataService.FileService.UpdateFile(file);
            return value;
        }

        [Transaction]
        public ActionResult Update(int[] dicts, int[] files)
        {

            var dictItems = dicts.Select(dictId => DataService.Dictionary.GetById(dictId)).ToList();

            foreach (var fileId in files)
            {
                var file = DataService.FileService.GetFileById(fileId);

                foreach (var dictionaryItem in dictItems)
                {
                    file.RelatedDictionaryItems.Add(dictionaryItem);
                }
                
                DataService.FileService.UpdateFile(file);
            }
            return RedirectToAction("Index");
        }
    }
}
